<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>鲜花类型</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="/res/js/jquery-3.4.1.min.js"></script>
    <link rel="stylesheet" href="/res/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-inline" style="margin-top: 10px">
    <label class="layui-form-label">类型名称</label>
    <div class="layui-show-lg-inline">
        <input type="text" id="lname" lay-verify="title" autocomplete="of">
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm" id="queryBtn">查询</button>
        </div>
    </div>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>


<script src="/res/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<script>
    var table=null;
    layui.use(['table'], function(){
        table = layui.table;
        table.render({
            elem: '#test'
            ,url:'/lvTb/TypeList'
            ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,id:'typeTable'
            ,page: true//开启分页
            ,totalRow: true //开启合计行
            ,cols: [[
                {type:'checkbox'}
                ,{field:'lid', width:180, title: 'ID', sort: true}
                ,{field:'lname', width:180, title: '类型名'}
                ,{field:'lremark', width:180, title: '类型注解'}
                ,{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}
            ]]
        });
        $("#queryBtn").on("click",function (){
            table.reload('typeTable',{
                page:{
                    curr: 1
                }
                ,where:{
                    lName:$("#lname").val(),
                }
            })
        });
        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    // layer.msg('添加');
                    //iframe层

                    var indexOpen=layer.open({
                        type: 2,
                        title: '类型维护',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['580px', '90%'],
                        content: '/pages/typeFrom.html' //iframe的url
                    });
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        // layer.alert('编辑 [id]：'+JSON.stringify(checkStatus.data[0]));
                        layer.open({
                            type: 2,
                            title: '类型维护',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['580px', '90%'],
                            content: '/pages/typeFrom.html?id='+checkStatus.data[0].lid+'' //iframe的url
                        });
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        layer.msg('删除');
                        let ids="";
                        for (let i=0;i< data.length;i++){
                            ids+=data[i].lid+",";
                        }
                        console.info("第一次的"+ids);
                        ids=ids.substring(0,ids.length-1);
                        console.info("第二次的"+ids);
                        let url="/lvTb/deleteBatch";
                        let data_={ids:ids};
                        $.post(url,data_,function (resp){
                            if (resp.state=="1"){
                                layer.msg(resp.msg);
                                table.reload('typeTable');
                            }else{
                                layer.msg(resp.msg);
                            }
                        },"json");
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'update'){
                // layer.msg('修改操作');
                layer.open({
                    type: 2,
                    title: '类型维护',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['580px', '90%'],
                    content: '/pages/typeFrom.html?id='+data.lid+'' //iframe的url
                });
            } else if(layEvent === 'delete'){
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                    let url="/lvTb/delete";
                    let data_={id:data.lid};
                    $.post(url,data_,function (resp){
                        if (resp.state=="1"){
                            layer.msg(resp.msg);
                            table.reload('typeTable');
                        }else{
                            layer.msg(resp.msg);
                        }
                    },"json")
                });
            }
        });

    });
    function closeLayer(msg){
        layer.closeAll();//关闭所有弹出层
        table.reload('typeTable');
        layer.msg(msg);
    }

</script>


</body>
</html>